package org.jeecg.modules.basic.print.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.basic.print.entity.PrintConfig;
import org.jeecg.modules.basic.print.entity.WmsDocumentTypeFields;
import org.jeecg.modules.basic.print.entity.WmsDocumentTypeStatus;
import org.jeecg.modules.basic.print.entity.WmsMaterialFields;
import org.jeecg.modules.basic.print.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    //
 * 创建人员:    zhaojun
 * 创建时间:    2023-09-28
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class PrintConfigUtilServiceImpl implements IPrintConfigUtilService {

    private final IPrintConfigService printConfigService;

    private final IWmsDocumentTypeFieldsService wmsDocumentTypeFieldsService;

    private final IWmsDocumentTypeStatusService wmsDocumentTypeStatusService;

    private final IWmsMaterialFieldsService wmsMaterialFieldsService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void copy(String copyId, String businessType, String documentType) {
        if (StringUtils.isBlank(copyId)) {
            throw new JeecgBootException("复制业务id【copyId】不能为空");
        }
        if (StringUtils.isBlank(businessType)) {
            throw new JeecgBootException("目标业务类型【businessType】不能为空");
        }
        if (StringUtils.isBlank(documentType)) {
            throw new JeecgBootException("目标单据类型【documentType】不能为空");
        }
        PrintConfig printConfig = printConfigService.getById(copyId);
        if (ObjectUtil.isEmpty(printConfig)) {
            throw new JeecgBootException("根据当前copyId未找到，打印配置主表记录");
        }
        // 删除所有的历史记录配置
        List<PrintConfig> list = printConfigService.list(new LambdaQueryWrapper<PrintConfig>().
                eq(PrintConfig::getBusinessType, businessType).eq(PrintConfig::getDocumentType, documentType));
        for (PrintConfig config : list) {
            wmsDocumentTypeFieldsService.remove(new LambdaQueryWrapper<WmsDocumentTypeFields>().eq(WmsDocumentTypeFields::getPrintConfigId, config.getId()));
            wmsDocumentTypeStatusService.remove(new LambdaQueryWrapper<WmsDocumentTypeStatus>().eq(WmsDocumentTypeStatus::getPrintConfigId, config.getId()));
            wmsMaterialFieldsService.remove(new LambdaQueryWrapper<WmsMaterialFields>().eq(WmsMaterialFields::getPrintConfigId, config.getId()));
            printConfigService.removeById(config.getId());
        }

        PrintConfig copyPrintConfig = new PrintConfig();
        BeanUtils.copyProperties(printConfig, copyPrintConfig, "id", "createTime", "updateTime");
        copyPrintConfig.setBusinessType(businessType);
        copyPrintConfig.setDocumentType(documentType);
        printConfigService.save(copyPrintConfig);

        List<WmsDocumentTypeFields> documentTypeFields = wmsDocumentTypeFieldsService.lambdaQuery().eq(WmsDocumentTypeFields::getPrintConfigId, copyId).list();
        if (CollectionUtil.isEmpty(documentTypeFields)) {
            log.info("当前字段配置为空，PrintConfigId：{}", copyId);
        } else {
            List<WmsDocumentTypeFields> copyDocumentTypeFields = new ArrayList<>();
            WmsDocumentTypeFields copyFields = null;
            for (WmsDocumentTypeFields fields : documentTypeFields) {
                copyFields = new WmsDocumentTypeFields();
                BeanUtils.copyProperties(fields, copyFields, "id", "createTime", "updateTime");
                copyFields.setPrintConfigId(copyPrintConfig.getId());
                copyDocumentTypeFields.add(copyFields);
            }
            wmsDocumentTypeFieldsService.saveBatch(copyDocumentTypeFields);
        }

        List<WmsDocumentTypeStatus> documentTypeStatus = wmsDocumentTypeStatusService.lambdaQuery().eq(WmsDocumentTypeStatus::getPrintConfigId, copyId).list();
        if (ObjectUtil.isEmpty(documentTypeStatus)) {
            log.info("当前业务配置为空，PrintConfigId：{}", copyId);
        } else {
            List<WmsDocumentTypeStatus> copyDocumentTypeStatus = new ArrayList<>();
            WmsDocumentTypeStatus copyStatus = null;
            for (WmsDocumentTypeStatus status : documentTypeStatus) {
                copyStatus = new WmsDocumentTypeStatus();
                BeanUtils.copyProperties(status, copyStatus, "id", "createTime", "updateTime");
                copyStatus.setPrintConfigId(copyPrintConfig.getId());
                copyDocumentTypeStatus.add(copyStatus);
            }
            wmsDocumentTypeStatusService.saveBatch(copyDocumentTypeStatus);
        }

        List<WmsMaterialFields> wmsMaterialFields = wmsMaterialFieldsService.lambdaQuery().eq(WmsMaterialFields::getPrintConfigId, copyId).list();
        if (ObjectUtil.isEmpty(wmsMaterialFields)) {
            log.info("当前业务配置为空，PrintConfigId：{}", copyId);
        } else {
            List<WmsMaterialFields> copyWmsMaterialFields = new ArrayList<>();
            WmsMaterialFields copyFields = null;
            for (WmsMaterialFields fields : wmsMaterialFields) {
                copyFields = new WmsMaterialFields();
                BeanUtils.copyProperties(fields, copyFields, "id", "createTime", "updateTime");
                copyFields.setPrintConfigId(copyPrintConfig.getId());
                copyWmsMaterialFields.add(copyFields);
            }
            wmsMaterialFieldsService.saveBatch(copyWmsMaterialFields);
        }
    }
}
